package com.example.springbooksystem.controller;


import com.example.springbooksystem.constant.Constants;
import com.example.springbooksystem.model.*;
import com.example.springbooksystem.service.BookService;
import com.example.springbooksystem.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description： 控制层: 接收前端发送的请求，对请求进⾏处理，并响应数据
 * 控制层解析请求, 向逻辑处理层拿服务
 *
 * @author: zxj
 * @date: 2023-11-07
 * @time: 23:05:15
 */
@RequestMapping("/book")
@RestController
@Slf4j
public class BookController {
    // 注入依赖, 从 Spring 中去 BookService 对象
    @Autowired
    BookService bookService;
    // BookService bookService = new BookService();

    @RequestMapping("/getListByPage")
    public ResponseData getList(PageRequest pageRequest, HttpSession session) {
        // 日志信息
        log.info("接收到查询翻页信息, 获取图书信息, pageRequest: {}", pageRequest);

        // // 判断是否登录
        // UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
        // if (userInfo == null || userInfo.getId() < 0 || "".equals(userInfo.getUserName())) {
        //     return ResponseData.unLogin(null);
        // }

        // 参数校验
        if (pageRequest.getPageSize() < 0 || pageRequest.getPageSize() < 1) {
            log.warn("参数校验失败");
            return ResponseData.fatal("参数校验失败");
        }

        // 获取数据
        PageResult<BookInfo> bookList = bookService.getBookList(pageRequest);

        return ResponseData.success(bookList);
    }

    @RequestMapping("/addBook")
    public ResponseData addBook(BookInfo bookInfo, HttpSession session) {
        log.info("接受到添加图书的请求, bookInfo: {}", bookInfo);

        // 判断是否登录
        // UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
        // if (userInfo == null || userInfo.getId() < 0 || "".equals(userInfo.getUserName())) {
        //     return ResponseData.unLogin(null);
        // }


        // 1. 参数是否合法检验
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null || bookInfo.getCount() < 0
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getPrice() == null) {
            log.warn("参数有问题, 请重新输入");
            return ResponseData.fatal("参数有问题, 请重新输入");
        }

        // 2. 开始业务处理
        Integer ret = bookService.addBook(bookInfo);
        if (ret <= 0) {
            log.error("添加图书失败, bookInfo: {}", bookInfo);
            return ResponseData.fatal("添加图书失败");

        }
        return ResponseData.success(null);
    }

    @RequestMapping("/queryById")
    public ResponseData queryById(Integer bookId, HttpSession session) {
        // 日志信息
        log.info("接收到查询图书ID信息的请求, ID: {}", bookId);

        // 判断是否登录
        // UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
        // if (userInfo == null || userInfo.getId() < 0 || "".equals(userInfo.getUserName())) {
        //     return ResponseData.unLogin(null);
        // }

        // 参数检验
        if (bookId == null || bookId < 1) {
            log.warn("参数请求出错");
            return ResponseData.fatal("参数请求出错");
        }
        // 逻辑处理
        BookInfo bookInfo = bookService.queryById(bookId);
        if (bookInfo == null) {
            log.warn("依据 id 查询图书信息失败");
            return ResponseData.fatal("依据 id 查询图书信息失败");
        }
        ResponseData responseData = new ResponseData();
        return ResponseData.success(bookInfo);
    }

    @RequestMapping("/updateBook")
    public ResponseData updateBook(BookInfo bookInfo, HttpSession session) {
        log.info("接收到更新图书的需求, bookInfo: {}", bookInfo);
        // 判断是否登录
        // UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
        // if (userInfo == null || userInfo.getId() < 0 || "".equals(userInfo.getUserName())) {
        //     return ResponseData.unLogin(null);
        // }
        Integer ret = bookService.updateBook(bookInfo);
        if (ret == null || ret < 1) {
            log.warn("更新图书出错, 请联系管理员");
            return ResponseData.fatal("更新图书出错, 请联系管理员");
        }
        return ResponseData.success(null);
    }

    @RequestMapping("/batchDeleteBook")
    public ResponseData batchDeleteBook(@RequestParam List<Integer> ids, HttpSession session) {
        log.info("批量删除图书, ids: {}", ids);

        // 判断是否登录
        // UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
        // if (userInfo == null || userInfo.getId() < 0 || "".equals(userInfo.getUserName())) {
        //     return ResponseData.unLogin(null);
        // }

        // 参数检验
        if (ids == null) {
            log.warn("批量删除图书传的参数有问题");
            return ResponseData.fatal("批量删除图书传的参数有问题");
        }

        // 业务处理
        Integer result = bookService.batchDeleteBook(ids);
        if (result == null || result <= 0) {
            log.warn("批量删除图书失败");
            return ResponseData.fatal("批量删除图书失败");
        }
        return ResponseData.success(null);
    }


}
